home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utmisc1 / alcun101.lha / src / romio.c < prev    next >
C/C++ Source or Header  |  1995-01-12  |  5KB  |  219 lines

  1. /*
  2.  *  This file is part of x48, an emulator of the HP-48sx Calculator.
  3.  *  Copyright (C) 1994  Eddie C. Dost  (ecd@dressler.de)
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* $Log: romio.c,v $
  21.  * Revision 1.1  1995/01/11  18:11:25  ecd
  22.  * Initial revision
  23.  *
  24.  *
  25.  * $Id: romio.c,v 1.1 1995/01/11 18:11:25 ecd Exp ecd $
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <sys/stat.h>
  32.  
  33. #include "global.h"
  34. #include "resources.h"
  35. #include "romio.h"
  36.  
  37. unsigned int opt_gx = 0;
  38. unsigned int rom_size = 0;
  39.  
  40. int
  41. #ifdef __FunctionProto__
  42. read_rom_file(char *name, unsigned char **mem, int *size)
  43. #else
  44. read_rom_file(name, mem, size)
  45. char *name;
  46. unsigned char **mem;
  47. int *size;
  48. #endif
  49. {
  50.   struct stat st;
  51.   FILE *fp;
  52.   unsigned char *tmp_mem;
  53.   unsigned char byte;
  54.   unsigned char four[4];
  55.   int i, j;
  56.  
  57.   *mem = NULL;
  58.   *size = 0;
  59.   if (NULL == (fp = fopen(name, "r")))
  60.     {
  61.       fprintf(stderr, "can\'t open %s\n", name);
  62.       return 0;
  63.     }
  64.  
  65.   if (stat(name, &st) < 0)
  66.     {
  67.       fprintf(stderr, "can\'t stat %s\n", name);
  68.       fclose(fp);
  69.       return 0;
  70.     }
  71.  
  72.   if (fread(four, 1, 4, fp) != 4)
  73.     {
  74.       fprintf(stderr, "can\'t read first 4 bytes of %s\n", name);
  75.       fclose(fp);
  76.       return 0;
  77.     }
  78.  
  79.   if (four[0] == 0x02 && four[1] == 0x03 &&
  80.       four[2] == 0x06 && four[3] == 0x09)
  81.     {
  82.       *size = st.st_size;
  83.     }
  84.   else if (four[0] == 0x32 && four[1] == 0x96 &&
  85.       four[2] == 0x1b && four[3] == 0x80)
  86.     {
  87.       *size = 2 * st.st_size;
  88.     }
  89.   else
  90.     {
  91.       fprintf(stderr, "%s is not a HP48 ROM\n", name);
  92.       fclose(fp);
  93.       return 0;
  94.     }
  95.  
  96.   if (fseek(fp, 0, 0) < 0)
  97.     {
  98.       fprintf(stderr, "can\'t fseek to position 0 in %s\n", name);
  99.       *size = 0;
  100.       fclose(fp);
  101.       return 0;
  102.     }
  103.  
  104.   *mem = (unsigned char *)malloc(*size);
  105.  
  106.   if (st.st_size == *size)
  107.     {
  108.       /*
  109.        * size is same as memory size, old version file
  110.        */
  111.       if (fread(*mem, 1, (size_t)*size, fp) != *size)
  112.         {
  113.           fprintf(stderr, "can\'t read %s\n", name);
  114.           free(*mem);
  115.           *mem = NULL;
  116.           *size = 0;
  117.           fclose(fp);
  118.           return 0;
  119.         }
  120.     }
  121.   else
  122.     {
  123.       /*
  124.        * size is different, check size and decompress memory
  125.        */
  126.  
  127.       if (st.st_size != *size / 2)
  128.         {
  129.           fprintf(stderr, "strange size %s, expected %d, found %ld\n",
  130.                   name, *size / 2, st.st_size);
  131.           free(*mem);
  132.           *mem = NULL;
  133.           *size = 0;
  134.           fclose(fp);
  135.           return 0;
  136.         }
  137.  
  138.       if (NULL == (tmp_mem = (unsigned char *)malloc((size_t)st.st_size)))
  139.         {
  140.           for (i = 0, j = 0; i < *size / 2; i++)
  141.             {
  142.               if (1 != fread(&byte, 1, 1, fp))
  143.                 {
  144.                   fprintf(stderr, "can\'t read %s\n", name);
  145.                   free(*mem);
  146.                   *mem = NULL;
  147.                   *size = 0;
  148.                   fclose(fp);
  149.                   return 0;
  150.                 }
  151.               (*mem)[j++] = byte & 0xf;
  152.               (*mem)[j++] = (byte >> 4) & 0xf;
  153.             }
  154.         }
  155.       else
  156.         {
  157.           if (fread(tmp_mem, 1, (size_t)*size / 2, fp) != *size / 2)
  158.             {
  159.               fprintf(stderr, "can\'t read %s\n", name);
  160.               free(*mem);
  161.               *mem = NULL;
  162.               *size = 0;
  163.               fclose(fp);
  164.               free(tmp_mem);
  165.               return 0;
  166.             }
  167.  
  168.           for (i = 0, j = 0; i < *size / 2; i++)
  169.             {
  170.               (*mem)[j++] = tmp_mem[i] & 0xf;
  171.               (*mem)[j++] = (tmp_mem[i] >> 4) & 0xf;
  172.             }
  173.     
  174.           free(tmp_mem);
  175.         }
  176.     }
  177.  
  178.   fclose(fp);
  179.  
  180.   if ((*mem)[0x29] == 0x00)
  181.     {
  182.       if (*size == ROM_SIZE_GX)
  183.         {
  184.           opt_gx = 1;
  185.         }
  186.       else
  187.         {
  188.           fprintf(stderr, "%s seems to be G/GX ROM, but size is 0x%x\n",
  189.           name, *size);
  190.           free(*mem);
  191.           *mem = NULL;
  192.           *size = 0;
  193.           return 0;
  194.         }
  195.     }
  196.   else
  197.     {
  198.       if (*size == ROM_SIZE_SX)
  199.         {
  200.           opt_gx = 0;
  201.         }
  202.       else
  203.         {
  204.           fprintf(stderr, "%s seems to be S/SX ROM, but size is 0x%x\n",
  205.           name, *size);
  206.           free(*mem);
  207.           *mem = NULL;
  208.           *size = 0;
  209.           return 0;
  210.         }
  211.     }
  212.  
  213.   if (verbose)
  214.     printf("%s: read %s\n", progname, name);
  215.  
  216.   return 1;
  217. }
  218.  
  219.